<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-top: 30px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="memberTypeName" label="名称：">
          <el-input v-model="filters.memberTypeName" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="描述：" label-width="100px">
          <el-input v-model="filters.remark" placeholder="描述"></el-input>
        </el-form-item>
        <el-row style="text-align: right">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
        </el-form-item>
          <el-form-item>
            <kt-button :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
          </el-form-item>
          <el-form-item >
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
              </el-tooltip>
              <el-tooltip content="列显示" placement="top">
                <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
              </el-tooltip>
              <el-tooltip content="导出" placement="top">
                <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
              </el-tooltip>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      v-loading="loading"
      :columns="filterColumns"
      :showBatchDelete="false"
      :showDeleteOperation="false"
      :showSelection="false"
      @findPage="findPage"
      @handleEdit="handleEdit"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="分组名称" prop="memberTypeName">
          <el-input v-model="dataForm.memberTypeName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员标签" prop="remark">
          <el-input v-model="dataForm.remark" auto-complete="off" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="忠诚度" prop="remark">
          <el-checkbox-group v-model="checkList">
            <el-checkbox label="普通会员"></el-checkbox>
            <el-checkbox label="高级会员"></el-checkbox>
            <el-checkbox label="VIP会员"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/JicTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      checkList: ['普通会员','高级会员'],
      size: "small",
      filters: {
        remark: "",
        memberTypeName: ""
      },
      columns: [
        { prop: "memberTypeName", label: "分组名称", minWidth: 100 },
        { prop: "remark", label: "会员标签", minWidth: 120 },
        { prop: "memberLevelNum", label: "包含标签数", minWidth: 80 },
        { prop: "memberNum", label: "会员数量", minWidth: 80 },
        { prop: "createTime", label: "创建时间", minWidth: 120 }
	  ],
	  filterColumns:[],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      loading: false,
      dataFormRules: {
        memberTypeName: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
        remark: [{ required: true, message: "请输入类型描述", trigger: "blur" }]
      },
      // 新增编辑界面数据
      dataForm: {
        id: "",
        memberTypeName: "",
        remark: ""
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function (data) {
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this_.loading = true
      this.utils.request.queryMemberTypeList(this_.filters, function (res) {
        if (res.data.rows == null) {
          res.data.rows = [];
        }
        this_.$refs.CyTable.findPage({content:res.data.rows,total:res.data.total});
        this_.loading = false
      });
    },
    // 显示新增界面
    handleAdd: function() {
      this.editDialogVisible = true;
      this.operation = true;
      this.resetForm("dataForm");
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.editDialogVisible = true;
      this.operation = false;
      this.$nextTick(function() {
        this.dataForm = Object.assign({}, params.row);
      });
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            var this_ = this;
            this.utils.request.editMemberTypeInfo(this.dataForm, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
                this_.$refs["dataForm"].resetFields();
                this_.editDialogVisible = false;
                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.message,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
        this.findPage(null);
      }
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },
  mounted() {
	  this.initColumns()
  }
};
</script>

<style scoped>
</style>
